<template>
  <div class="view-wrapper">
    <div class="head">
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="店铺名称：">
          <el-input v-model="formInline.shopName" placeholder="请输入"></el-input>
        </el-form-item>
        <el-form-item label="团购名称：">
          <el-input v-model="formInline.name" placeholder="请输入"></el-input>
        </el-form-item>
        <el-form-item label="团购状态：">
          <el-select v-model="formInline.status" placeholder="请选择">
            <el-option v-for="item in options" :key="item.id" :label="item.value" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button class="sumit_button" @click="search">查询</el-button>
          <el-button @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="listBox">
      <div class="listBox_head">
        <el-button type="primary" class="sumit_button" @click="addBuy">新增团购</el-button>
      </div>
      <ex-table ref="table" :loading="loading" :table-column="tableColumn" :table-data="tableData"
        @pageSizeChange="handleSizeChange" @currentPageChange="handleCurrentChange">
        <template v-slot:operate="{ scope }">
          <el-button type="primary" size="small" @click="toExamine(scope.row.id)" v-if="scope.row.status==2">审核
          </el-button>
          <el-button type="" size="small" @click="editJump(scope.row.id)">编辑</el-button>
          <el-button type="danger" size="small" @click="deactivate(scope.row.id)"
            v-if="scope.row.status==3||scope.row.status==4">停用</el-button>
        </template>
      </ex-table>
    </div>
    <detail-dialog :id="dialogId" v-bind="$attrs" v-if="dialogVisible" :visible.sync="dialogVisible"
      @success="_handleSuccess" />
  </div>
</template>
<script>
  // components
  import {
    adminDict,
    grouponList,
    stopGroupon
  } from "@/api/operateActive";
  import ExportExcelButton from "@/components/ExportExcelButton/index";
  import DetailDialog from "./components/DetailDialog";
  import handlePageMixin from "@/mixins/handlePageMixin";
  import {
    mapGetters
  } from "vuex";
  export default {
    name: "billList",
    components: {
      ExportExcelButton,
      DetailDialog,
    },
    mixins: [handlePageMixin],
    data() {
      return {
        rowKey:'id',
        loading: false,
        dialogId: '', //审核id
        formInline: {
          name: "",
          status: "",
          shopName: "",
        },
        dialogVisible: false,
        addCount: "", //增发数量
        date: [],
        options: [],
        tableData: [],
        tableColumn: [{
            prop: "id",
            label: "团购id"
          },
          {
            prop: "shopName",
            label: "店铺名称",
            width: 180
          },
          {
            prop: "name",
            label: "团购名称",
            width: 180
          },
          {
            prop: "activeTime",
            label: "活动时间",
            width: 180
          },
          {
            prop: "useTime",
            label: "使用时间",
            width: 180
          },
          {
            prop: "stock",
            label: "商品库存"
          },
          {
            prop: "grouponPrice",
            label: "团购价格"
          },
          {
            prop: "originalPrice",
            label: "原价"
          },
          {
            prop: "used",
            label: "总销量"
          },
          {
            prop: "writeOff",
            label: "已核销"
          },
          {
            prop: "refund",
            label: "退款数量",
            width: 120
          },
          {
            prop: "statusDict",
            label: "团购状态",
            width: 120
          },
          {
            prop: "updateTime",
            label: "修改时间",
            width: 160
          },
          {
            label: "操作",
            slot: true,
            slotName: "operate",
            width: 240,
            fixed: "right",
          },
        ],
      };
    },
    computed: {
      ...mapGetters(["isChain"]),
    },
    created() {},
    mounted() {
      this.getlinkagelist();
      this.getList();
    },
    methods: {
      rowChange(e){
        console.log('9999',e)
        return e.id
      },
      selectChange(e) { //表格多选
        console.log(e)
      },
      //获取字典
      getlinkagelist() {
        const params = {
          dictType: "merchant_groupon_status",
        };
        adminDict(params).then((res) => {
          var arr = [{
            id: "",
            value: "全部",
          }, ];
          this.options = arr.concat(res.result[0].dictList);
        });
      },
      toExamine(id) {
        //审核
        this.dialogId = id
        this.dialogVisible = true;
      },
      _handleSuccess(e) {
        //审核成功回调
        this.reset()
      },
      addBuy() {
        //新增团购
        this.$router.push({
          path: "./add-active",
        });
      },
      editJump(id) {
        //编辑团购
        this.$router.push({
          path: "./add-active?id=" + id,
        });
        // this.$router.push({
        //   path: "./edit-active",
        // });
      },
      deactivate(id) {
        //停用
        this.$confirm("是否停用？", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          })
          .then(() => {
            stopGroupon(id).then((res) => {
              this.$message({
                message: "停用成功",
                type: "success",
                duration: 1500,
              });
              setTimeout(() => {
                this.reset();
              }, 800);
            });
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消",
            });
          });
      },
      search() {
        //搜索
        console.log(this.formInline);
        this.pageOptions.pageNum = 1;
        this.$refs.table.pageOptions.pageNum = 1;
        this.getList();
      },
      reset() {
        this.formInline = {};
        this.pageOptions.pageNum = 1;
        this.$refs.table.pageOptions.pageNum = 1;
        this.getList();
      },
      getList() {
        const params = {
          ...this.pageOptions,
          ...this.formInline,
        };
        this.loading = true;
        grouponList(params)
          .then((res) => {
            const {
              total,
              records
            } = res.result;
            records.forEach((item) => {
              item.activeTime = item.activityStartTime + ' 至 ' + item.activityEndTime
              item.useTime = item.useStartTime + ' 至 ' + item.useEndTime
              item.grouponPrice = '￥' + item.grouponPrice
              item.originalPrice = '￥' + item.originalPrice
            });
            this.tableData = records;
            this.$refs.table.pageOptions.total = total;
          })
          .finally(() => {
            this.loading = false;
          });
      },
    },
  };

</script>
<style lang="scss" scoped>
  .el-card-title {
    font-size: 20px;
  }

  .head {
    //   box-shadow: 0px 4px 12px 0px rgb(1 41 99 / 6%);
    background-color: #fff;
    border-radius: 8px;
    //   height: 120px;
    padding: 20px 20px 0;
  }

  .el-form--inline .el-form-item {
    margin-right: 40px;
  }

  .listBox {
    //   box-shadow: 0px 4px 12px 0px rgb(1 41 99 / 6%);
    background-color: #fff;
    border-radius: 8px;
    //   height: 120px;
    padding: 20px 20px;
    margin-top: 10px;
  }

  .table_list {
    display: flex;
    align-items: center;
    // border: 1px solid #DCDFE6;
    border-radius: 7px;
    width: 220px;
    margin-bottom: 10px;
  }

  .tableBox {
    width: calc(100% / 3);
    height: 36px;
    text-align: center;
    line-height: 36px;
    color: #303133;
    font-size: 14px;
    border: 1px solid #dcdfe6;
    border-right: 0;
  }

  .tableBox:first-child {
    border-radius: 7px 0px 0px 7px;
  }

  .tableBox:last-child {
    border-right: 1px solid #dcdfe6;
    border-radius: 0px 7px 7px 0px;
  }

  .spchoosetext {
    background-color: #fff4e6;
    color: #ff9300;
    // border: 1px solid #ff9300;
  }

  .listBox_head {
    margin-bottom: 20px;
    display: flex;
  }

  .slot_box {
    display: flex;
    align-items: center;
  }

  .avatar_img {
    width: 34px;
    height: 34px;
    border-radius: 50%;
    margin-right: 10px;
  }

  .sumit_button {
    color: #fff !important;
    background: #ff7b35 !important;
    border: 0;
  }

  .sumit_button:hover {
    opacity: 0.9;
  }

  .card_box {
    width: 160px;
    height: 100px;
    background: linear-gradient(112deg,
        #f85936 0%,
        rgba(255, 147, 123, 0.99) 100%);
    border-radius: 4px;
    font-size: 16px;
    font-weight: bold;
    color: #fff;
    text-align: center;
    line-height: 100px;
  }

</style>
